using System;
using System.Collections.Generic;
using DG.Tweening;
using GameFramework.Battle.Core;
using Neonagee.LocalPreferences;
using UnityEngine;
using Yoozoo.Framework.Managers;
using Yoozoo.Gta.Common;
using Random = System.Random;

namespace Gameplay.PVE
{
    public class PveUIDataManager
    {
        private string playerName;
        private string timeKey;
        private string allMoneyKey;
        private string uncollectedMoneyKey;
        private string itemListKey;

        public int GetAwardTimeStamp;
        public int AllMoney;
        public int UnCollectedMoney;
        public Dictionary<int,int> itemList = new Dictionary<int,int>();
        public int[] LuaItemList;

        private TimeManager.PveTween tween;
        public void Init()
        {
            playerName = ResidentHandler.Inst.GetFunction("pveManager.GetPlayerNickName").Call()[0].ToString();
            timeKey = playerName + "_pveGetAwardTime";
            GetAwardTimeStamp = LocalPrefs.GetInt(timeKey);
            if (GetAwardTimeStamp == 0)
            {
                RefreshGetAwardTime();
                GetAwardTimeStamp = LocalPrefs.GetInt(timeKey);
            }
            allMoneyKey = playerName + "_pveAllMoney";
            AllMoney = LocalPrefs.GetInt(allMoneyKey);
            if (AllMoney == 0)
            {
                RefreshAllMoney(10000);
            }
            uncollectedMoneyKey = playerName + "_pveUncollectedMoney";
            UnCollectedMoney = LocalPrefs.GetInt(uncollectedMoneyKey);

            var ticks = (int)DateTime.Now.Ticks - GetAwardTimeStamp;
            //UnCollectedMoney += ticks * 200;
            
            itemListKey = playerName + "_pveItemList";
            var str = LocalPrefs.GetString(itemListKey);
            if (string.IsNullOrEmpty(str))
            {
                
            }
            else
            {
                UpdateItemList(str);
            }
            LuaItemList = new int[12];
            foreach (var item in itemList)
            {
                LuaItemList[item.Key] = item.Value;
            }
        }

        private void UpdateItemList(string str)
        {
            itemList.Clear();
            var strList = str.Split(';');
            for (int i = 0; i < strList.Length; i++)
            {
                var strList2 = strList[i].Split('|');
                int itemIndex;
                int itemCount;
                int.TryParse(strList2[0],out itemIndex);
                int.TryParse(strList2[1], out itemCount);
                if (!itemList.ContainsKey(itemIndex))
                {
                    itemList.Add(itemIndex,itemCount);
                }
                else
                {
                    itemList[itemIndex] += itemCount;                
                }
            }
        }

        public void RefreshItemList()
        {
            int count = 0;
            string temp = string.Empty;
            foreach (var item in itemList)
            {
                count++;
                string itemStr = string.Format("{0}|{1}",item.Key,item.Value);
                temp += itemStr;
                if (count != itemList.Count)
                {
                    temp += ";";
                }
            }
            LocalPrefs.SetString(itemListKey,temp);
        }

        public void RefreshGetAwardTime()
        {
            var currentTime = (int)DateTime.Now.Ticks;
            LocalPrefs.SetInt(timeKey,currentTime);
        }

        public void RefreshUnCollectedMoney(int money)
        {
            UnCollectedMoney = money;
            LocalPrefs.SetInt(uncollectedMoneyKey,UnCollectedMoney);
        }
        
        public void RefreshAllMoney(int money)
        {
            AllMoney = money;
            LocalPrefs.SetInt(allMoneyKey,AllMoney);
        }

        public void AddUnCollectedMoney(int money)
        {
            UnCollectedMoney += money;
            RefreshUnCollectedMoney(UnCollectedMoney);
            ResidentHandler.Inst.GetFunction("pveManager.RefreshUncollectedMoney").Action(UnCollectedMoney);
        }

        public void AddAllMoney(int money)
        {
            AllMoney += money;
            RefreshAllMoney(AllMoney);
            ResidentHandler.Inst.GetFunction("pveManager.RefreshAllMoney").Action(AllMoney);
        }

        public void AddAward()
        {
            if (UnityEngine.Random.Range(0, 1f) > 0.1f)
            {
                return;
            }
            int index = UnityEngine.Random.Range(0, 12);
            if (!itemList.ContainsKey(index))
            {
                itemList.Add(index, 1);
            }
            else
            {
                itemList[index]++;
            }
            RefreshAward();
        }

        public void RefreshAward()
        {
            RefreshItemList();
            LuaItemList = new int[12];
            foreach (var item in itemList)
            {
                LuaItemList[item.Key] = item.Value;
            }
            ResidentHandler.Inst.GetFunction("pveManager.RefreshAward").Action(LuaItemList);
        }

        public void CollectMoney()
        {
            int startMoney = AllMoney;
            //AddAllMoney(UnCollectedMoney);
            AllMoney += UnCollectedMoney;
            UnCollectedMoney = 0;
            
            RefreshAllMoney(AllMoney);
            RefreshUnCollectedMoney(UnCollectedMoney);
            if (tween != null)
            {
                TimeManager.Instance.Kill(tween);
                tween = null;
            }
            tween = TimeManager.Instance.Tween((percent) =>
            {
                ResidentHandler.Inst.GetFunction("pveManager.RefreshAllMoney").Action((int)Mathf.Lerp(startMoney, AllMoney, percent));
            },1);
            
            
            ResidentHandler.Inst.GetFunction("pveManager.RefreshUncollectedMoney").Action(UnCollectedMoney);
            ResidentHandler.Inst.GetFunction("pveManager.RefreshAwardTick").Call();
            itemList.Clear();
            RefreshAward();
            RefreshGetAwardTime();
        }
    }
}